<?php
/**
 * 获取实例类或数据等
 * 
 * @package SFramework
 * @author Shuky
 * @since v1.0
 * @version $Id: Obtain.php 82 2010-12-23 18:23:35Z shukyoo@gmail.com $
 */
namespace SFramework;

class Obtain
{
	/**
	 * 数据库实例
	 */
	public static function getDb()
	{
		require_once 'Zend/Db.php';
		$config = Registry::get('confDb');
		$adapter = $config['adapter'];
		unset($config['adapter']);
		return \Zend_Db::factory($adapter, $config);
	}


	/**
	 * Memcache实例
	 */
	public static function getMemcached()
	{
		return self::setCache('memcached');
	}

	
	/**
	 * 日志类实例
	 */
	public static function getLoger()
	{
		include_once APP_ROOT .'/biz/loger.class.php';
		return new Loger();
	}


	/**
	 * IP数据
	 */
	public static function getIp()
	{
		if ( getenv('HTTP_CLIENT_IP') ) {
			$ip = getenv( 'HTTP_CLIENT_IP' );
		} elseif ( getenv('HTTP_X_FORWARDED_FOR') ) {
			$ip = getenv( 'HTTP_X_FORWARDED_FOR' );
		} elseif ( getenv('REMOTE_ADDR') ) {
			$ip = getenv( 'REMOTE_ADDR' );
		} else {
			$ip = $_SERVER['REMOTE_ADDR'];
		}
        return $ip;
	}



	protected static function setCache($name)
	{
		$name = strtolower($name);
		$classFile = APP_ROOT .'/biz/cache/'. $name .'.class.php';
		$className = 'Cache_'. ucfirst($name);
		if (!is_file($classFile)) {
			require_once APP_ROOT .'/biz/exception.class.php';
			throw new Biz_Exception('指定的缓存类不存在');
		}
		require_once $classFile;
		$cacheConfig = include_once APP_ROOT .'/biz/config/cache.php';
		return new $className($cacheConfig[$name]);
	}
}